package dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import businnes.Client;


@Transactional
public class ClientDAOJPA implements ClientDAO {

	@PersistenceContext
	private EntityManager entityManager;

	public Client get(Long clientId) {
		return entityManager.find(Client.class, clientId);
	}

	public Long save(Client client) {
		entityManager.persist(client);
		return client.getClientId();
	}

	public void update(Client client) {
		entityManager.merge(client);
	}

	public List<Client> list() {
		Query query = entityManager.createQuery("FROM Client");
		return query.getResultList();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

}
